import Vue from 'vue'
import http from '@/api'
import config from '@/global/config'
import Router from 'vue-router'
import permission from './permission'
import workRouter from './work-router'
Vue.use(Router)

// 路由白名单
const whiteList = config.whiteList

export const constantRoutes = [
	{
		path: '/login',
		component: () => import(/* webpackChunkName: "login" */ '@/views/login'),
		name: 'login',
		hidden: true,
		meta: {
			title: '登录'
		}
	},
	{
		path: '/404',
		component: () => import(/* webpackChunkName: "404" */ '@/views/error/404'),
		name: '404',
		hidden: true,
		meta: {
			title: '404'
		}
	},
	{
		path: '/403',
		component: () => import(/* webpackChunkName: "403" */ '@/views/error/403'),
		name: '403',
		hidden: true,
		meta: {
			title: '403'
		}
	},
	{
		path: '*',
		redirect: '/404'
	}
	// {
	// 	path: '/activiti/definition/edit',
	// 	component: () => import(/* webpackChunkName: "modeler" */ '@/views/activiti/modeler/edit'),
	// 	hidden: true,
	// 	name: 'edit',
	// 	meta: {
	// 		title: '流程绘制'
	// 	}
	// }
]
export const allRouter = [...constantRoutes, ...workRouter]
const createRouter = () =>
	new Router({
		// mode: 'history',
		scrollBehavior: () => ({ y: 0 }),
		routes: $config.needFeatchMenu ? constantRoutes : allRouter,
		// linkExactActiveClass: "footer-nav-active"
	})

const router = createRouter()

// 重置路由
export function resetRouter() {
	if (!$config.needFeatchMenu) {
		return
	}
	const newRouter = createRouter()
	router.matcher = newRouter.matcher
}
// 设置路由权限
permission(router, whiteList)

router.beforeEach((to, from, next) => {
	http.cleanPendingRequest() // 路由跳转清空所有未完成的请求
	next()
})

export default router
